package io.github.iogogogo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.github.iogogogo.commons.util.DateTimeUtils;
import io.github.iogogogo.commons.util.FileUtils;
import io.github.iogogogo.commons.util.IdHelper;
import io.github.iogogogo.commons.util.JsonParse;
import io.github.iogogogo.enums.LevelEnum;
import io.github.iogogogo.model.AlertModel;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * Created by tao.zeng on 2025/3/21.
 */
@Slf4j
public class MockDataMain {

    public static void main(String[] args) throws IOException {
        List<@Nullable AlertModel> list = Lists.newArrayList();

        Map<String, Object> extendMap = Maps.newHashMap();
        extendMap.put("extend1", DateTimeUtils.nowDateTime());
        extendMap.put("extend2", IdHelper.uuid());
        extendMap.put("extend3", IdHelper.id());

        String source = "alert_mock_data";

        AlertModel main = AlertModel.builder()
                .id(IdHelper.id()).timestamp(DateTimeUtils.nowDateTime().minusSeconds(5L)).source("").level(LevelEnum.LEVEL_1.getLevel())
                .business("信用卡积分权益系统").content("oracle活动会话数增长告警").hostIp("21.99.116.170").host("NODE01").source(source)
                .extendMap(extendMap)
                .build();
        list.add(main);

        AlertModel alert1 = AlertModel.builder()
                .id(IdHelper.id()).timestamp(DateTimeUtils.nowDateTime().minusSeconds(3L)).source("").level(LevelEnum.LEVEL_2.getLevel())
                .business("信用卡积分权益系统").content("cims, Metrics Process limit %isat 83.45，告警类型:WARNING").hostIp("21.99.116.2").host("NODE02").source(source)
                .extendMap(extendMap)
                .build();

        AlertModel alert2 = AlertModel.builder()
                .id(IdHelper.id()).timestamp(DateTimeUtils.nowDateTime()).source("").level(LevelEnum.LEVEL_2.getLevel())
                .business("信用卡积分权益系统").content("服务器 23.93118.790 ping不通，请关注!最新值0，【Ping.系统管理员处理】").hostIp("21.99.116.1").host("NODE03").source(source)
                .extendMap(extendMap)
                .build();

        AlertModel alert3 = AlertModel.builder()
                .id(IdHelper.id()).timestamp(DateTimeUtils.nowDateTime().minusSeconds(2L)).source("").level(LevelEnum.LEVEL_1.getLevel())
                .business("信用卡积分权益系统").content("cims, The Data Guard status of cims is Error ORA-16198Timeout incurred on internalchannel during remote archival.")
                .hostIp("21.99.116.3").host("NODE04").source(source).extendMap(extendMap)
                .build();


        list.add(alert1);
        list.add(alert2);
        list.add(alert3);

        Map<String, Object> map = Maps.newHashMap();
        map.put("data", list);
        map.put("key", "7705ed3140ded161ecba27e2d3eaaf72");

        String filepath = String.join(File.separator, System.getProperty("user.dir"), "logs", "alert_mock_api_data.json");
        if (new File(filepath).exists()) Files.delete(Paths.get(filepath));
        FileUtils.writeString(filepath, JsonParse.tryToJson(map));


        filepath = String.join(File.separator, System.getProperty("user.dir"), "logs", "alert_mock_kafka_data.json");
        if (new File(filepath).exists()) Files.delete(Paths.get(filepath));
        kafkaData(filepath, JsonParse.tryToJson(main));
        kafkaData(filepath, JsonParse.tryToJson(alert1));
        kafkaData(filepath, JsonParse.tryToJson(alert2));
        kafkaData(filepath, JsonParse.tryToJson(alert3));
    }

    private static void kafkaData(String filepath, String content) throws IOException {
        FileUtils.writeString(filepath, content);
    }

}